import 'package:safetymonitor/app/components/custom_app_bar.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_title_chart_page.dart';
import 'package:safetymonitor/app/components/custom_refresh.dart';
import 'package:safetymonitor/app/components/dialog/exit_tips.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/widget_extension.dart';
import 'package:safetymonitor/app/data/entities/application/maintenance_logs_entity.dart';

import 'controller.dart';

/// 维护日志
class MaintainLogsPage extends StatelessWidget {
  MaintainLogsPage({super.key});

  final controller = Get.put(MaintainLogsController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            backgroundColor: AppColors.bg_page_color,
            appBar: CustomAppBar(
              titleText: '维护日志',
              actions: [
                //新增
                IconButton(
                  icon: const Icon(Icons.add),
                  onPressed: controller.addMaintainLogs,
                ),
              ],
            ),
            body: Container(
              color: AppColors.bg_color_grey,
              padding: EdgeInsets.only(top: 12.w, right: 14.w, left: 14.w),
              child: Column(
                children: [
                  Expanded(
                      child: CustomEasyRefreshList<MaintenanceLogsEntity>(
                    key: controller.globalKey,
                    dataLoad: controller.getMaintenanceLogsList,
                    itemBuilder: (item, index) {
                      return _itemWidget(item);
                    },
                  ))
                ],
              ),
            ),
          );
        });
  }

  /// 标题
  Widget _title(String title, String? value,
      {Color color = const Color(0xff495061)}) {
    return Row(
      children: [
        Text(
          title,
          style: TextStyle(
            color: const Color(0xffA5A6B5),
            fontSize: 13.sp,
            fontWeight: FontWeight.w400,
          ),
        ),
        if (value != null)
          Expanded(
              child: Text(
            value,
            style: TextStyle(
              color: color,
              fontSize: 13.sp,
              fontWeight: FontWeight.w400,
            ),
          )),
      ],
    );
  }

  Widget _itemWidget(MaintenanceLogsEntity item) {
    return CustomTitleChartPage(
        title: '操作人员：${item.operators}',
        child: Column(
          children: [
            Row(
              children: [
                Expanded(
                  child: _title(
                      '操作内容：',item.maintenanceRecords,
                  ),
                ),
                Expanded(
                  child: _title(
                      '添加人员：',item.recordPersonnel,
                  ),
                ),
              ],
            ),
            AppGaps.vGap5,
            _title(
              '操作时间：',
              item.operationTime.isNotEmpty? item.operationTime.replaceAll('T', ' '): '',
            ),
            AppGaps.vGap10,
            _delAndEdit(item),
          ],
        ));
  }


  Widget _delAndEdit(MaintenanceLogsEntity item) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 10.w),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8),
        color: const Color(0x1A5C6DFF),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          _topTitle('编辑','assets/svg/icon_double_edit.svg').clickOnce(() {
            controller.onClickEdit(item);
          }),
          _divider(),
          _topTitle('删除','assets/svg/icon_double_delete.svg').clickOnce(() {
            Get.dialog<bool>(ExitTips(
              content: '是否确定删除？',
              func: (v) {
                if (v) {
                  controller.delLog(item.id);
                }
              },
            ));
          }),
        ],
      ),
    );
  }
  ///分割线
  Widget _divider() {
    return Container(
      width: 1.w,
      height: 12.w,
      decoration: const BoxDecoration(
          gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
            Color(0xFFFFFFFF),
            Color(0xFFFFFFFF),
          ])),
    );
  }
  Widget _topTitle(String title, String icon) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        SvgPicture.asset(icon),
        AppGaps.hGap4,
        Text(
          title,
          style: TextStyle(
            fontSize: 14.sp,
            color: const Color(0xFF5C6DFF),
            height: 1.2,
            fontWeight: FontWeight.w400,
          ),
        ),
      ],
    );
  }
}
